package cn.lingyangwl.agile.system.manager;

import cn.lingyangwl.agile.system.model.entity.SysRoleMenu;
import cn.lingyangwl.framework.tool.core.CollectionUtils;
import cn.lingyangwl.agile.system.mapper.SysRoleMenuRelMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author shenguangyang
 */
@Component
public class SysRoleMenuManager {
    @Resource
    private SysRoleMenuRelMapper sysRoleMenuRelMapper;

    /**
     * 通过角色ID删除角色和菜单关联
     *
     * @param roleId 角色ID
     * @return 结果
     */
    public int deleteByRoleId(Long roleId) {
        if (Objects.isNull(roleId)) {
            return 0;
        }
        LambdaQueryWrapper<SysRoleMenu> wrapper = Wrappers.<SysRoleMenu>lambdaQuery()
                .eq(SysRoleMenu::getRoleId, roleId);
        return sysRoleMenuRelMapper.delete(wrapper);
    }

    /**
     * 批量删除角色菜单关联信息
     *
     * @param roleIds 需要删除的数据ID
     * @return 结果
     */
    public boolean deleteByRoleIds(List<Long> roleIds) {
        if (CollectionUtils.isEmpty(roleIds)) {
            return false;
        }
        LambdaQueryWrapper<SysRoleMenu> wrapper = SysRoleMenu.lqw()
                .in(SysRoleMenu::getRoleId, roleIds);
        return sysRoleMenuRelMapper.delete(wrapper) > 0;
    }
}
